package pinboard.dataLayer.potocol.op;

import android.util.Base64;

import java.io.IOException;

import pinboard.dataLayer.RefBag;
import pinboard.dataLayer.binPack.BinSerializer;
import pinboard.dataLayer.potocol.renderFormat.AddClipResult;
import pinboard.dataLayer.potocol.renderFormat.AddGroupResult;
import pinboard.dataLayer.potocol.renderFormat.AddRemoveElementResult;
import pinboard.dataLayer.potocol.renderFormat.AddRemoveStrokeResult;
import pinboard.dataLayer.potocol.renderFormat.ClearStrokeResult;
import pinboard.dataLayer.potocol.renderFormat.ClipTransResult;
import pinboard.dataLayer.potocol.renderFormat.ElementTransResult;
import pinboard.dataLayer.potocol.renderFormat.RemoveGroupResult;
import pinboard.dataLayer.potocol.renderFormat.InitResult;
import pinboard.dataLayer.potocol.renderFormat.RawHeadParam;
import pinboard.dataLayer.potocol.renderFormat.RemoveClipResult;

public class RenderClientDecoder {
    public static RawHeadParam decode(byte[] bag, RefBag<Integer> offset) throws IOException {
        RawHeadParam hp = BinSerializer.toObject(bag, offset, RawHeadParam.class);
        return hp;
    }

    public static void decode(byte[] bag, RenderClientDecodeListener listener) {
        //copyToStr(bag);  //打开这行可以把byte捕捉到base64字符串后续方便调试静态数据
        RefBag<Integer> offset = new RefBag<>();
        offset.data = 0;
        if (listener != null) {
            RawHeadParam rhp = null;
            try {
                rhp = decode(bag, offset);
                invokeListener(rhp, bag, offset, listener);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    private static void invokeListener(RawHeadParam rhp, byte[] bag, RefBag<Integer> offset, RenderClientDecodeListener listener) throws IOException {
        if (rhp.opType == 1) {
            if (rhp.opCode == 1) { //數據初始化協議解析
                InitResult result = BinSerializer.toObject(bag, offset, InitResult.class);
                listener.onInit(rhp, result);
            } else if (rhp.opCode == 5) { //清除所有筆畫
                ClearStrokeResult result = BinSerializer.toObject(bag, offset, ClearStrokeResult.class);
                listener.onClearStroke(rhp, result);
            } else if (rhp.opCode == 7) { //新增頁數據協議解析
                AddClipResult result = BinSerializer.toObject(bag, offset, AddClipResult.class);
                listener.onAddClip(rhp, result);
            } else if (rhp.opCode == 8) { //刪除頁
                RemoveClipResult result = BinSerializer.toObject(bag, offset, RemoveClipResult.class);
                listener.onRemoveClip(rhp, result);
            } else if (rhp.opCode == 10) { //新增Group
                AddGroupResult result = BinSerializer.toObject(bag, offset, AddGroupResult.class);
                listener.onAddGroup(rhp, result);
            } else if (rhp.opCode == 11) {  //刪除group
                RemoveGroupResult result = BinSerializer.toObject(bag, offset, RemoveGroupResult.class);
                listener.onRemoveGroup(rhp, result);
            } else if (rhp.opCode == 12) { //頁形變,放大镜，旋转好像没有吧
                ClipTransResult result = BinSerializer.toObject(bag, offset, ClipTransResult.class);
                listener.onClipTrans(rhp, result);
            } else if (rhp.opCode == 13) {//新增刪除筆畫
                AddRemoveStrokeResult result = BinSerializer.toObject(bag, offset, AddRemoveStrokeResult.class);
                listener.onAddRemoveStroke(rhp, result);
            } else if (rhp.opCode == 15) { //新增刪除元素(目前只有圖片)協議 TODO怎样测新增图片？据说暂停使用，但谈不上作废
                AddRemoveElementResult result = BinSerializer.toObject(bag, offset, AddRemoveElementResult.class);
                listener.onAddRemoveElement(rhp, result);
            } else if (rhp.opCode == 16) { //元素形變
                ElementTransResult result = BinSerializer.toObject(bag, offset, ElementTransResult.class);
                listener.onElementTrans(rhp, result);
            }
        }
    }


    //把byte捕捉到base64字符串后续方便调试静态数据
    private String copyToStr(byte[] bag) {
        String base64 = null;
        try {
            base64 = android.util.Base64.encodeToString(bag, Base64.DEFAULT);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (base64.length() < 0) {
            base64 = "";
        }
        return base64;
    }
}
